import {
    getAllThemeComment,
    deleteThemeComment,
    themeCommentList,
    themeCommentDetail,
    checkThemeComment
} from '../services/fetch'
import { message } from 'antd';
import { deepClone } from '../utils/request';

export default {

    namespace: 'themecomment',

    state: {
        pagination: {      //列表页分页
            total: 0,
            pageSize: 10,
            current: 1,
            index: 1,
        },
        loading: false, //列表加载状态,
        selectedRowKeys: [],
        selectedPlateIds: [],//已选中的稿件id集合
        selectedPlateDatas: [],//已选中稿件data集合
        commentPagination: {  //一级评论分页
            information_id: '',
            total: 0,
            page_size: 10,
            index: 1
        },
        visibleModal: false,
        currentInformationData:{},
        dataSource: [],
        childList:{
            data:[],
            pagination: {
                total: 0,
                pageSize: 10,
                current: 1,
            },
        },
        commentList: [],   //一级评论列表
    },

    subscriptions: {
        setup({ dispatch, history }) {
        },
    },

    effects: {
        //删除评论列表
        *deleteComment({ data }, { call, put, select }) {
            const res = yield call(deleteThemeComment, data);
            if (res.data.code === 200) {
                message.success(res.data.msg);
                let pagination = yield select((state) => {
                    return state.themecomment.pagination
                })
                const list = yield call(getAllThemeComment, pagination)
                yield put({ type: 'init', payload: list.data.data.list });
            }
        },

        //审核评论
        *checkComment({ data }, { call, put, select }) {
            const res = yield call(checkThemeComment, data);
            if (res.data.code === 200) {
                message.success(res.data.msg);
                let pagination = yield select((state) => {

                    return state.themecomment.pagination
                })
                const list = yield call(getAllThemeComment, pagination)
                yield put({ type: 'init', payload: list.data.data.list });
            }
        },
        //删除一级评论
        *deleteFirstComment({ data }, { call, put, select }) {
            const res = yield call(deleteThemeComment, data);
            if (res.data.code === 200) {
                message.success(res.data.msg);
                yield put({ type: 'remove', payload: {
                        comment_id:data.comment_id
                    } });
                let currentInformationData = yield select((state) => {
                    return state.themecomment.currentInformationData
                })
                currentInformationData.comment_count = currentInformationData.comment_count - res.data.data.delete_count;

                yield put({ type: 'currentInformationData', payload: currentInformationData });



            }

        },

        //删除二级评论
        *deleteDetailComment({ data }, { call, put, select }) {
            const res = yield call(deleteThemeComment, data);
            if (res.data.code === 200) {
                message.success(res.data.msg);
                yield put({ type: 'removeDetail', payload: {
                        comment_id:data.comment_id,
                        key:data.key
                    } });
                let currentInformationData = yield select((state) => {
                    return state.themecomment.currentInformationData
                });
                let commentList = yield select((state) => {
                    return state.themecomment.commentList
                });

                commentList[data.key]['child_count'] -= 1;

                currentInformationData.comment_count = currentInformationData.comment_count - 1;

                yield put({ type: 'currentInformationData', payload: currentInformationData });
                yield put({ type: 'commentListData', payload: commentList });


            }
        },
        //审核评论
        *checkFirstComment({ data }, { call, put, select }) {
            const res = yield call(checkThemeComment, data);
            if (res.data.code === 200) {
                message.success(res.data.msg);
                let commentList = yield select((state) => {
                    return state.themecomment.commentList
                });
                commentList[data.key].status = data.status;

                yield put({ type: 'commentListData', payload: commentList });

            }
        },
        //审核评论
        *checkDetailComment({ data }, { call, put, select }) {
            const res = yield call(checkThemeComment, data);
            if (res.data.code === 200) {
                message.success(res.data.msg);
                if(data.status === 1){
                    document.getElementById('comment'+data.comment_id).innerHTML='审核通过';
                    document.getElementById('comment'+data.comment_id).style.color="#4caf50";
                }else{
                    document.getElementById('comment'+data.comment_id).innerHTML='拒审';
                    document.getElementById('comment'+data.comment_id).style.color="#f44336";
                }

            }
        },
        //所有评论列表
        *getAllComment({ data }, { call, put, select }) {
            const list = yield call(getAllThemeComment, data)
            yield put({ type: 'init', payload: list.data.data.list });

            let pagination = { ...data, total: list.data.data.total, current: data.index }
            yield put({ type: 'pagination', payload: pagination });
        },
        //一级评论列表
        *commentList({ payload }, { call, put, select }) {
            let data = payload;
            data.pid = 0;

            const list = yield call(getAllThemeComment, data);

            let commentPagination = { ...data, total: list.data.data.total, index: payload.index }
            yield put({ type: 'currentInformationData', payload: list.data.data.info });
            yield put({ type: 'commentPagination', payload: commentPagination });
            let old = yield select((state) => {
                return state.themecomment.commentList
            })
            let newList = [];
            //翻页合并数组
            if (data.index == 1){
                newList = list.data.data.list;
            } else{
                newList = old.concat(list.data.data.list);
            }

            yield put({ type: 'commentListData', payload: newList })


            if (newList.length > 0){
                let data = {
                    key:0,
                    comment_id:newList[0].comment_id,
                    index:1,
                    page_size:10
                }

                yield put({ type: 'commentDetail', payload: data })
            }




        },
        //二级评论列表
        *commentDetail({ payload }, { call, put, select }) {

            let old = yield select((state)=>{

                return state.themecomment.commentList
            })

            let data = {
                comment_id:payload.comment_id,
                index:payload.index,
                page_size:payload.page_size
            }
            let list = yield call(themeCommentDetail,data)

            //将查询出来的数据，存放到一级评论列表中去, 分页时合并数组,合并时检测是否为已存在数据
            let childData = [];

            if(old && old[payload.key].child && old[payload.key].child.data){

                let flag = true;
                childData = old[payload.key].child.data;
                childData.forEach((item)=>{

                    if(list.data.data.list.length > 0 && list.data.data.list[0].comment_id == item.comment_id){
                        flag = false;
                    }
                });
                if(flag){

                    childData = old[payload.key].child.data.concat(list.data.data.list)
                }
            }else{
                childData = list.data.data.list;

            }

            old[payload.key].child = {
                pagination:{
                    index:payload.index,
                    page_size:payload.page_size,
                    total:list.data.data.total,
                },
                data:childData
            }

            yield put({type:'commentListData', payload: old })
        },
        *showModal({ payload }, { call, put, select }) {
            let pagination = yield select((state) => {
                return state.themecomment.commentPagination
            })
            payload.index = 1;
            payload.page_size = pagination.page_size;
            yield put({ type: 'commentListData', payload: [] })
            yield put({ type: 'commentList', payload });
            yield put({ type: 'showModalVisible' });
        }
    },


    reducers: {
        currentInformationData(state, action){
            return { ...state, currentInformationData: action.payload };
        },
        init(state, action) {
            return { ...state, dataSource: action.payload };
        },
        removeDetail(state,{payload}){
            let data = state.commentList[payload.key].child.data;
            data = data.filter(todo => todo.comment_id !== payload.comment_id)
            state.commentList[payload.key].child.data = data;
           return { ...state, commentList:state.commentList };
        },
        remove(state, {payload}) {
            let news = state.commentList.filter( todo => todo.comment_id !==payload.comment_id);
            return { ...state, commentList:news };
        },
        commentDetailRe(state, action) {
            return { ...state, commentList: action.payload };
        },
        commentPagination(state, action) {
            return { ...state, commentPagination: action.payload };
        },
        pagination(state, action) {
            return { ...state, pagination: action.payload };
        },
        commentListData(state, action) {
            return { ...state, commentList: action.payload };
        },
        showModalVisible(state, action) {
            return { ...state, visibleModal: true }
        },
        hideModal(state, action) {
            return { ...state, visibleModal: false }
        },
        setSelectedPlateIds(state, action) {
            return { ...state, selectedPlateIds: action.data.ids, selectedPlateDatas: action.data.list };
        },
        updateMoreDeal(state, action) {
            return { ...state, selectedRowKeys: action.data };
        },
    },

};
